﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlServerCe;

namespace ProvaBD
{
    public partial class Form1 : Form
    {
        BD bd;
        public Form1()
        {
            InitializeComponent();
            bd = new BD();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            inserirEnLaGrid();
        }
        public void inserirEnLaGrid()
        {
            bd.Connectarse();
            AlumnesConversor a = new AlumnesConversor();
            SqlCeDataReader r = a.mostratTots();

            bd.propertyConexxio.Close();
            //Netajem el grid
            for (int i = dataGridView1.Rows.Count - 1; i >=1; i--)
            {

                dataGridView1.Rows.RemoveAt(0);

            }
            while (r.Read())
            {
                //Creo una fila
                Object[] arr = new Object[4];
                arr[0] = r["nMatricula"];
                arr[1] = r["Nom"];
                arr[2] = r["Cognom"];
                arr[3] = r["Telefon"];
                //Afegeixo la fila al GridView
                dataGridView1.Rows.Add(arr);
            }
            r.Close();
        }
        private void usuariToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MostrarUsuari();
           
        }
        public void MostrarUsuari()
        {
            AlumneConversor a = new AlumneConversor();
            DataGridViewSelectedRowCollection fSel = dataGridView1.SelectedRows;
            if (fSel.Count > 0)
            {
                a.propertyTelefon=a.obtenirTelefon((int)fSel[0].Cells[0].Value);
                //Carregar objecte
                a.propertyMatricula = ((int)fSel[0].Cells[0].Value);
                a.propertyNom = fSel[0].Cells[1].Value.ToString();
                a.propertyCognom = fSel[0].Cells[2].Value.ToString();
            }
            //Mostrar el formulari
            Form2 f = new Form2();

            //Set AlumneConversor
            f.setAlumne(a);
            f.ShowDialog();
            inserirEnLaGrid();
        }
        private void usuariToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AgregarUsuari f = new AgregarUsuari();
            f.ShowDialog();
            inserirEnLaGrid();
        }

        private void eliminarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AlumneConversor a = new AlumneConversor();
            DataGridViewSelectedRowCollection fSel = dataGridView1.SelectedRows;
             if (fSel.Count > 0)
             {
                 a.propertyMatricula = ((int)fSel[0].Cells[0].Value);
             }
             a.eliminarAlumne();
             inserirEnLaGrid();
        }
    }
}
